package com.spzx.common.rabbit.config;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.rabbit.entity.GmallCorrelationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 发送端消息确认配置类
 */
@Slf4j
@Component
public class RabbitInitConfigApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        this.setupCallbacks();
    }

    private void setupCallbacks() {
        /**
         * 只确认消息是否正确到达 Exchange 中,成功与否都会回调
         *
         * @param correlation 相关数据  非消息本身业务数据
         * @param ack         应答结果
         * @param reason      如果发送消息到交换器失败，错误原因
         */
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, reason) -> {
            if (ack) {
                // 消息成功投递到交换机
                log.info("消息发送到Exchange成功：{}", correlationData);
            } else {
                // 消息投递交换机失败
                log.error("消息发送到Exchange失败：{}", reason);
                // 执行消息重发

            }
        });
        /**
         * 消息没有正确到达队列时触发回调，如果正确到达队列不执行
         */
        this.rabbitTemplate.setReturnsCallback(returned -> {
            log.error("returned：" + returned.getMessage());
            log.error("replyCode：" + returned.getReplyCode());
            log.error("replyText：" + returned.getReplyText());
            log.error("exchange/routingKey：" + returned.getExchange() + "/" + returned.getRoutingKey());
        });
    }

    /**
     * 消息重新发送
     *
     * @param correlationData
     */
    private void retrySendMsg(CorrelationData correlationData) {
        // 获取相关数据
        GmallCorrelationData gmallCorrelationData = (GmallCorrelationData) correlationData;
        // 获取redis中存放重试次数，先重发，再将次数写回到redis中
        int retryCount = gmallCorrelationData.getRetryCount();
        if (retryCount >= 3) {
            // 超过最大重试次数
            log.error("生产者超过最大重试次数，将失败的消息存入数据库人工处理；给管理员发送提示，申请人工处理。");
            return;
        }
        // 重发消息
        rabbitTemplate.convertAndSend(gmallCorrelationData.getExchange(),
                gmallCorrelationData.getRoutingKey(),
                gmallCorrelationData.getMessage(),
                gmallCorrelationData);
        // 重发次数+1
        retryCount += 1;
        gmallCorrelationData.setRetryCount(retryCount);
        redisTemplate.opsForValue().set(gmallCorrelationData.getId(),
                JSON.toJSONString(gmallCorrelationData),
                10,
                TimeUnit.MINUTES);
        log.info("进行消息重发");
    }
}
